// 引入接口文件
import { updateCoupon, deleteGift, exportGift, importCoupon,queryAllAddress,checkCode,okVerific } from './giftListServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

import { calculate_object_name } from 'utils/upload'
import { makeAddress } from "utils/util"

// 初始默认状态
const defultState = {
  exportLoading: false,
  loading: false,
  areaList:[],
  checkBtn:true,
  btnLoading: false,
  actionType:'',
	biz: {
		queryForm: {}
	},
	modalObj:{
		visible:false,
	  },
	codeList:{},//验证码返回数据
	codeQuery:{}//验证码
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					// 执行业务操作
					// dispatch({
					// 	type: 'queryInfo',
					// 	payload: {}
					// }) // end dispatch
					dispatch({ type: 'getAreaList', payload: {} })
				} // end if
			}) // end listen
		},
	},

	// 方法列表
	effects: {
		//领取码验证
		*doCheckCode({ payload }, { put, call, select }){
			try{
				var {checkBtn} = yield select(state => state[tmpModule.namespace])
				const {success,data} = yield call(checkCode,payload.userGiftId)
				if(success){
					yield put({ type: 'updateStore', payload: {codeList:data, checkBtn:!checkBtn, } });
				}
			}catch(e){console.log(e)}
		},
		//核销礼品码
		*doOkVerific({ payload }, { put, call, select }){
			var {codeList,codeQuery,modalObj,biz,checkBtn} = yield select(state => state[tmpModule.namespace])
			const {success,data} = yield call(okVerific,{...codeQuery,receive_count:codeList.limitCount})
			if(success){
				message.success('提取成功！');
				modalObj.visible = !modalObj.visible;
				
				yield put({ type: 'updateStore', payload: { modalObj,checkBtn:!checkBtn,codeList:{},codeQuery:{} } });
				biz.refreshListDatas(true)
			}
			// console.log(modalObj)
		},
		//获取级联列表
		*getAreaList({ payload }, { put, call, select }) {
			try {
			  const { data, success } = yield call(queryAllAddress);
			  if (success) {
				let areaList = [];
				let arr = [data[0]]
				let attr = [data[data.length - 1]]
				data[0] = {
				  label: "北京",
				  value: "110100",
				  children: arr
				}
				data[data.length - 1] = {
				  label: "澳门",
				  value: "820000",
				  children: attr
				}
				//去掉第三级，
				areaList = makeAddress(data, 2);
				
				yield put({ type: 'updateStore', payload: { areaList: areaList } });
			  }
			} catch (e) {
	  
			}
		  },
		*dowmCoupon({ payload }, { put, call, select }) {
			try {
				let state = yield select(state => state[tmpModule.namespace])
				const { data, success, msg } = yield call(updateCoupon, payload.params)
				if (success) {
					message.success('操作成功')
					state.biz.refreshListDatas(true)
				}
			} catch (e) {
				console.error(e)
			}
		},

		// *deleteGift({ payload }, { put, call, select }) {
		// 	try {
		// 		let state = yield select(state => state[tmpModule.namespace])
		// 		const { data, success, msg } = yield call(deleteGift, payload.giftId)
		// 		if (success) {
		// 			message.success('操作成功')
		// 			state.biz.refreshListDatas(true)
		// 		}
		// 	} catch (e) {
		// 		console.error(e)
		// 	}
		// },

		*exportGift({ payload }, { put, call, select }) {
			try {
				let state = yield select(state => state[tmpModule.namespace])
				const { data, success, msg } = yield call(exportGift, payload.params)
				if (success) {
					message.success('操作成功')
					payload.cb(data)
				}
			} catch (e) {
				console.error(e)
			}
		},

		*importCoupon({ payload }, { put, call, select }) {
			try {
				let state = yield select(state => state[tmpModule.namespace])
				const { data, success, msg } = yield call(importCoupon, payload)
				if (success) {
					message.success('操作成功')
					// payload.cb(data)
				}
			} catch (e) {
				console.error(e)
			}
		},

		*updateSign({ payload }, { put, call, select }) {
			try {
				let { signObj, file } = payload;
				let uploadObj = {};
				// 上传对象
				uploadObj = {
					host: signObj.host,
					params: {
						OSSAccessKeyId: signObj.accessid,
						policy: signObj.policy,
						signature: signObj.signature,
						key: calculate_object_name(signObj.dir, 'floor', file.name),
						name: file.name,
						success_action_status: '200'
					}
				}
				yield put({ type: 'updateStore', payload: { uploadObj } });
			} catch (e) {
				console.error(e);
			}
		},

	},

	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}

export default tmpModule
